﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SMS
{
    public partial class uThemtaikhoan : UserControl
    {
        fLogin main;
        public uThemtaikhoan(fLogin main1)
        {
            InitializeComponent();
            txtMaTK.Enabled = true;
            main = main1;
        }

        private void lblMatKhau_Click(object sender, EventArgs e)
        {

        }

        private void lblTenTaiKhoan_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void txtMaXe_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void uThemtaikhoan_Load(object sender, EventArgs e)
        {
            txtMaTK.Enabled = false;
        }


        private void bntLuu_Click(object sender, EventArgs e)
        {
            //KetNoiDataBase db = new KetNoiDataBase();
            SqlConnection conn = new SqlConnection(main.ConnectDB);
            SqlCommand command = new SqlCommand("SP_MATAIKHOAN", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Connection = conn;
            command.Parameters.Add(new SqlParameter("@TENTK", txtTenTK.Text.ToString()));
            command.Parameters.Add(new SqlParameter("@MATKHAUMD", txtMatKhau.Text.ToString()));


            if (txtTenTK.Text.Trim() == "")
            {
                MessageBox.Show("Nhập Tên tài khoản!");
            }
            else
            {
                if (txtMatKhau.Text.Trim() == "")
                {
                    MessageBox.Show("Nhâp mật khẩu!");
                }
                else
                {
                    
                            try
                            {
                                conn.Open();
                                command.ExecuteNonQuery();
                                //SqlCommand sc = new SqlCommand("INSERT INTO TAIKHOAN VALUES(N'" + txtMaTK.Text + "', N'" + txtTenTK.Text + "',N'" + txtMatKhau.Text + "')", conn);
                                //sc.ExecuteNonQuery();
                                MessageBox.Show("Thêm thành công", "thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                main.Danhsachtaikhoan.updateDSTK();
                            }
                            catch
                            {
                                MessageBox.Show("Tên Tài Khoản đã có trong hệ thống ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            finally
                            {
                                command.Dispose();
                                conn.Close();
                                txtTenTK.ResetText();
                                txtMatKhau.ResetText();
                                
                            }
                        }
                    }
                

            
            
        }

        private void bntHuy_Click(object sender, EventArgs e)
        {
            
            txtTenTK.Text = null;
            txtMatKhau.Text = null;
        }
    }
}
